//
//  XSDownloadBotView.m
//  XSDownloadDemo
//
//  Created by 大帅 on 2023/12/27.
//

#import "XSDownloadBotView.h"

@interface XSDownloadBotView ()
/** 全选 */
@property (nonatomic, strong) UIButton *allButton;
/** 删除 */
@property (nonatomic, strong) UIButton *delButton;

@end

@implementation XSDownloadBotView

-(UIButton *)allButton {
    if (!_allButton) {
        _allButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_allButton setTitle:@"全选" forState:UIControlStateNormal];
        [_allButton setTitle:@"取消" forState:UIControlStateSelected];
        _allButton.backgroundColor = UIColor.blackColor;
        _allButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _allButton.tag = 100;
        [_allButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _allButton;
}

-(UIButton *)delButton {
    if (!_delButton) {
        _delButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_delButton setTitle:@"删除" forState:UIControlStateNormal];
        _delButton.backgroundColor = UIColor.redColor;
        _delButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _delButton.tag = 101;
        [_delButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _delButton;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createViewUI];
    }
    return self;
}

-(void)createViewUI {
    [self addSubview:self.allButton];
    [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self);
        make.right.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(49);
    }];
    
    [self addSubview:self.delButton];
    [self.delButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self);
        make.left.mas_equalTo(self.allButton.mas_right);
        make.top.bottom.mas_equalTo(self.allButton);
    }];
}

-(void)clickButton:(UIButton *)sender {
    if (sender.tag == 100) {
        sender.selected = !sender.selected;
        // 全选 / 取消
        if (self.botViewAllSelectBlock) {
            self.botViewAllSelectBlock(sender.isSelected);
        }
    } else if (sender.tag == 101) {
        // 删除
        if (self.botViewDeleteBlock) {
            self.botViewDeleteBlock();
        }
    }
}

/// 全选按钮选中状态 / 当前选中删除数量
-(void)updateAllButtonSelect:(BOOL)sel delNum:(NSInteger)delNum {
    self.allButton.selected = sel;
    NSString *delStrNum = @"删除";
    if (delNum > 0) {
        delStrNum = [NSString stringWithFormat:@"删除(%ld)", delNum];
    }
    [self.delButton setTitle:delStrNum forState:UIControlStateNormal];
}


@end
